package org.rollinitiative.d20.combat.actions.standard;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rollinitiative.d20.combat.Action;
import org.rollinitiative.d20.combat.Combatant;
import org.rollinitiative.d20.dice.Dice;
import org.rollinitiative.d20.entity.Actor;
import org.rollinitiative.d20.entity.HitPoints.LifeState;
import org.rollinitiative.d20tools.arena.Arena;

public class Attack implements Action
{
    static final Log LOG_ = LogFactory.getLog(Attack.class);

    protected Combatant attacker_ = null;

    protected Combatant defender_ = null;

    protected Dice dice = Dice.getInstance();


    public Attack(Combatant attacker)
    {
        this.attacker_ = attacker;
    }


    public void setAttacker(Combatant attacker)
    {
        this.attacker_ = attacker;
    }


    public void setDefender(Combatant defender)
    {
        this.defender_ = defender;
    }


    public double calculate(Combatant combatant)
    {
        // Calculate near target, weapon drawn, etc...
        return 0.0;
    }


    public void attack(Arena arena, Combatant attacker, Combatant defender)
    {
        String attackerName = attacker_.getActor().getName();
        String defenderName = defender_.getActor().getName();
        // competitive roles
        LOG_.info(attackerName + " is attacking " + defenderName);
        int playerA = attacker_.meleeAttack();
        int playerB = defender_.defence();

        // determine outcome
        if (playerA >= playerB) {
            int dmg = dice.roll(1, 8, 0);
            LifeState lifeState = defender.getActor().damage(dmg);
            LOG_.info("--" + attackerName + " hits " + defenderName + " for " + dmg);
            if (lifeState != LifeState.ALIVE) {
                LOG_.info("----" + defenderName + " is dead or dying...");
                if (arena.getPartyList().contains(defender.getActor()))
                    arena.getPartyList().remove(defender.getActor());
                if (arena.getHostileList().contains(defender.getActor()))
                    arena.getHostileList().remove(defender.getActor());
            }
        }
        else if (playerB > playerA) {
            LOG_.info("--" + attackerName + " misses " + defenderName);
        }
    }


    public void execute()
    {
        String attackerName = attacker_.getActor().getName();
        String defenderName = defender_.getActor().getName();
        // competitive roles
        LOG_.info(attackerName + " is attacking " + defenderName);
        int playerA = attacker_.meleeAttack();
        int playerB = defender_.defence();

        // determine outcome
        if (playerA >= playerB) {
            int dmg = dice.roll(1, 8, 0);
            LifeState lifeState = defender_.getActor().damage(dmg);
            LOG_.info("--" + attackerName + " hits " + defenderName + " for " + dmg);
            if (lifeState != LifeState.ALIVE) {
                LOG_.info("----" + defenderName + " is dead or dying...");
                // if (arena.getPartyList().contains(defender.getActor()))
                // arena.getPartyList().remove(defender.getActor());
                // if (arena.getHostileList().contains(defender.getActor()))
                // arena.getHostileList().remove(defender.getActor());
                // }
            }
            else if (playerB > playerA) {
                LOG_.info("--" + attackerName + " misses " + defenderName);
            }
        }
    }
}
